<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class ScheduleUser_model extends CI_Model {
    
    public $tablename = 'hpg_schedule_user';
    
    
    public function __construct()
    {
        parent::__construct();
        // Your own constructor code
    }
    
    public function getUidList($schedule_id){
        if( !$schedule_id ){
            return false;
        }
        $list = $this->db->select('uid,status')->from($this->tablename)
        ->where('schedule_id='.$schedule_id)
        ->get_array();
        return $list;
    }
    
    public function update_batch($schedule_id, $uids){
        if( !$schedule_id ){
            return false;
        }
        $uids = array_filter($uids);
        $uids = array_unique($uids);
        //查询已经添加的人员
        $exsitusers = $this->db->select('uid')->from($this->tablename)
        ->where('schedule_id='.$schedule_id)
        ->fetchOnlyField('uid');
        $del_user = array_diff($exsitusers, $uids);
        $add_user = array_diff($uids, $exsitusers);
        if( $del_user ){
            $delres = $this->db->from($this->tablename)
            ->where('schedule_id='.$schedule_id)->where_in('uid', $del_user)->delete();
        }
        if( $add_user ){
            $data = [];
            foreach ($add_user as $v){
                $data[] = ['schedule_id'=>$schedule_id, 'uid'=>$v];
            }
            $addres = $this->insert_batch($data);
        }
    }
    
    public function insert_batch($list){
        if( !$list ){
            return false;
        }
        $res = $this->db->insert_batch($this->tablename, $list);
        return $res;
    }
    
    public function insert($data){
        if( !$data ){
            return false;
        }
        $res = $this->db->insert($this->tablename, $data);
        return $res;
    }
    
    public function update($map, $data){
        if( !$map || !$data ){
            return false;
        }
        $res = $this->db->update($this->tablename, $data, $map);
        return $res;
    }
    
    public function delete($map){
        if( !$map ){
            return false;
        }
        $res = $this->db->delete($this->tablename, $map);
        return $res;
    }
}
?>